package com.yjhb.android.sbz.v2.dialog

import android.view.View
import android.view.animation.Animation
import android.view.animation.ScaleAnimation
import androidx.fragment.app.Fragment
import com.yjhb.android.sbz.R
import com.yjhb.android.sbz.tool.SharePopup
import com.yjhb.android.sbz.v2.mvp.presenter.InvitationRewardPresenter
import com.yjhb.android.sbz.v2.mvp.view.InvitationRewardView
import com.jojo.android.zxlib.base.BaseDialog
import com.jojo.android.zxlib.tool.ZxSpanTool
import com.jojo.android.zxlib.tool.ZxToast
import kotlinx.android.synthetic.main.dialog_invitation_reward.*
import java.math.BigDecimal


/**
 *  创建者 zx
 *  创建时间 2021/5/19 10:27
 */
class InvitationRewardDialog(fragment: Fragment, var type: Int) : BaseDialog(fragment), InvitationRewardView {
    override fun getLayoutId() = R.layout.dialog_invitation_reward
    override fun init() {}

    private val presenter = lazy { InvitationRewardPresenter(this) }
    private var money: BigDecimal? = null

    init {
        resetUI()
        btnAnim()
        regEvent()
    }

    private fun regEvent() {
        close_iv?.setOnClickListener { dismiss() }
    }

    /**
     * 重置UI
     */
    private fun resetUI() {
        when (type) {
            //未邀请
            1 -> {
                depict_tv?.text = "您还未邀请徒弟"
                btn_tv?.text = "马上邀请"
                tips_tv?.visibility = View.VISIBLE
                btn_tv?.setOnClickListener {
                    dismiss()
                    SharePopup.showSharePopup2(activity, activity.window.decorView, 0)
                }
            }
            //等待拆红包
            2 -> {
                depict_tv?.text = "恭喜您获得红包"
                btn_tv?.text = "拆红包"
                tips_tv?.visibility = View.VISIBLE
                btn_tv?.setOnClickListener {
                    presenter.value.getNewUserReward()
                }
            }
            //红包已经拆开
            3 -> {
                val chars = "${money}元"
                depict_tv?.text = ZxSpanTool.ZoomSpan(chars, chars.lastIndex, chars.length, 0.5f)
                btn_tv?.text = "已拆完继续邀请"
                tips_tv?.visibility = View.INVISIBLE
                btn_tv?.setOnClickListener {
                    dismiss()
                    SharePopup.showSharePopup2(activity, activity.window.decorView, 0)
                }
            }
        }
    }

    /**
     * 按钮动画
     */
    private fun btnAnim() {
        val scaleAnimation1 = ScaleAnimation(0.9f, 1.1f, 0.9f, 1.1f,
                //以父控件中心开始缩放
                Animation.RELATIVE_TO_PARENT, 0.5f,
                Animation.RELATIVE_TO_PARENT, 0.5f)

        scaleAnimation1.duration = 1000
        //重复缩放
        scaleAnimation1.repeatMode = Animation.REVERSE
        //无限循环
        scaleAnimation1.repeatCount = -1
        btn_tv?.startAnimation(scaleAnimation1)
    }

    override fun onFailure(code: Int, msg: String?) {
        ZxToast.showBottomToast(msg)
    }

    override fun onSuccess(money: BigDecimal?) {
        this.money = money
        this.type = 3
        resetUI()
    }
}